package org.android.donottrack;

import org.android.donottrack.api.SupportFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

	private static final String TAG = "DoNotTrack";

	private static final String GOOGLE_LOCATION_APP = "com.google.android.location";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// boolean appInstall = isGoogleLocationAppInstalled();

		// ((TextView) findViewById(R.id.appInstalledTv))
		// .setText("Google Location app installed: " + appInstall);
		//
		// findViewById(R.id.clearLocHistBtn).setEnabled(appInstall);

		// ((CheckBox) findViewById(R.id.networkLocationCb))
		// .setOnClickListener(new View.OnClickListener() {
		// public void onClick(View v) {
		// boolean isChecked = ((CheckBox) v).isChecked();
		// try {
		// SupportFactory.getInstance(Main.this)
		// .setLocationProviderEnabled(
		// getContentResolver(),
		// LocationManager.NETWORK_PROVIDER,
		// isChecked);
		// } catch (SecurityException se) {
		// Toast.makeText(Main.this, R.string.no_permission,
		// Toast.LENGTH_SHORT).show();
		// showLocationSettings(null);
		// }
		// }
		// });
		// try {
		// Log.v(TAG,
		// "assisted_gps_enabled: "
		// + Settings.Secure.getInt(getContentResolver(),
		// "assisted_gps_enabled"));
		// Log.v(TAG,
		// "LOCATION_PROVIDERS_ALLOWED: "
		// + Settings.Secure.getString(getContentResolver(),
		// Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
		// boolean status = Settings.Secure.getString(getContentResolver(),
		// Settings.Secure.LOCATION_PROVIDERS_ALLOWED).contains(
		// NETWORK_PROVIDER);
		// String statusS = "Google Location Service: ";
		// statusS += status ? "ON" : "OFF";
		// ((TextView) findViewById(R.id.statusTv)).setText(statusS);
		// } catch (SettingNotFoundException e) {
		// Log.v(TAG, "error", e);
		// e.printStackTrace();
		// }
		
		if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.DONUT) {
            onCreatePreDonut();
    }

	}

	private void onCreatePreDonut() {
		findViewById(R.id.clearLocHistBtn).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showLocationAppSettings(v);
			}
		});
		
		findViewById(R.id.locationBtn).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showLocationSettings(v);
			}
		});
	}

	@Override
	protected void onResume() {
		Log.v(TAG, "onResume()");
		super.onResume();

		boolean locationProviderEnabled = SupportFactory.getInstance(this)
				.isLocationProviderEnabled(getContentResolver(),
						LocationManager.NETWORK_PROVIDER);
		((TextView) findViewById(R.id.serviceStatusTv))
				.setText(locationProviderEnabled ? "ON" : "OFF");
		// ((CheckBox) findViewById(R.id.networkLocationCb))
		// .setChecked(locationProviderEnabled);
		//
		// ((TextView) findViewById(R.id.networkLocationSummaryTv))
		// .setText(locationProviderEnabled ?
		// R.string.location_neighborhood_level
		// : R.string.location_networks_disabled);
	}

	private boolean isGoogleLocationAppInstalled() {
		final PackageManager packageManager = getPackageManager();
		for (PackageInfo pkg : packageManager.getInstalledPackages(0)) {
			if (pkg.packageName.equals(GOOGLE_LOCATION_APP)) {
				return true;
			}
		}
		return false;
	}

	public void showSecuritySettings(View v) {
		startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));
	}

	public void showLocationSettings(View v) {
		Toast.makeText(
				this,
				getString(R.string.change_settings_and_setting_name,
						getString(R.string.location_network_based)),
				Toast.LENGTH_LONG).show();
		startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
	}

	public void showLocationAppSettings(View v) {
		if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.GINGERBREAD) {
			Toast.makeText(this, R.string.clear_data_manually,
					Toast.LENGTH_LONG).show();
			startActivity(new Intent(
					Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
					Uri.parse("package:" + GOOGLE_LOCATION_APP)));
		} else {
			new AlertDialog.Builder(this).setMessage(R.string.find_app_and_clear_data_manually).setCancelable(true).setPositiveButton("OK", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					startActivity(new Intent(Settings.ACTION_APPLICATION_SETTINGS));
				}
			}).create().show();
			
//			Toast.makeText(this, R.string.find_app_and_clear_data_manually,
//					Toast.LENGTH_LONG).show();
			//startActivity(new Intent(Settings.ACTION_APPLICATION_SETTINGS));
		}

		// ActivityManager am = (ActivityManager)
		// getSystemService(Context.ACTIVITY_SERVICE);
		// boolean res = am.clearApplicationUserData(GOOGLE_LOCATION_APP);
	}

	public void showUninstallApp(View v) {
		Uri packageURI = Uri.parse("package:" + GOOGLE_LOCATION_APP);
		Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
		startActivity(uninstallIntent);
	}
}